/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.dhF9;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import com.example.luke.cashorderingsystem.Util.GlobalFunction;

import java.util.ArrayList;


public class dhF9Jni extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        /* Create a TextView and set its content.
         * the text is retrieved by calling a native
         * function.
         */
        TextView tv = new TextView(this);
      //  tv.setText( stringFromJNI() );

     //   tv.setText( "sumJNI="+ rtxt );
       // setContentView(tv);
    }

    public  static byte[] calculatePass(byte [] logicCardID){
        dhF9Jni obj=new dhF9Jni();
    byte [] password=new byte[]{(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff};

        int a=1;
        int b = 2;
        int sum = obj. sumJNI(a,b);
        //  tv.setText( "sumJNI="+ sum );
        int snnr[]={0x8b,0x25,0xc3,0x28};//685293915,0x28D8C15B

        for (int i=0;i<logicCardID.length;i++){
           Byte temp=logicCardID[i];
        //    GlobalFunction.printText("tag",String.valueOf(temp.intValue()));

            snnr[i]=temp.intValue();

        }
      //  GlobalFunction.printText("tag","snr");

        int ikey[] =  {0x78,0x90,0x12};//用户系统key

        int retb[]={0,0,0,0,0,0,};
        retb=obj.getKeyJNI(ikey,snnr);
        int i =0;
        while (i<6)
        {	a=retb[i];
            password[i]=(byte)a;

        //   GlobalFunction.printText("tag",String.valueOf(a));

            i++;
        }
       // GlobalFunction.printText("tag","pass");
   return password;

    }
    public static String calculateRegisCode(String content){
        int indata[] = {0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88};
        GlobalFunction.printTextByTag("con:"+content);
        ArrayList<Integer> tempArr=new ArrayList<>();

        for (int i=0;i<content.length();i=i+2){
            String temp=content.substring(i,i+2);
            GlobalFunction.printTextByTag(temp);
              String res=      GlobalFunction.convertToDecimal1(temp);

            tempArr.add(Integer.valueOf(res));

        }
        for (int i = 0; i <tempArr.size() ; i++) {
            indata[i]=tempArr.get(i);

        }


        dhF9Jni obj=new dhF9Jni();
        //
      int []  retKey = obj.mydes1encJNI(indata,null);
     int  i=0;
     int b=0;

        String result="";

        while (i<8)
        {	b=retKey[i];
        String temp=GlobalFunction.convertToHexString(b);

        result=result+temp;

            i++;
        }
        GlobalFunction.printTextByTag(result);
return result;

    }

    public native int sumJNI(int i, int j) ;

	/* A native method that is implemented by the
     * 'hello-jni' native library, which is packaged
     * with this application.
     */
    public native int[]  getKeyJNI(int[] ikey,int[] snr);
    public native String stringFromJNI();

    /* This is another native method declaration that is *not*
     * implemented by 'hello-jni'. This is simply to show that
     * you can declare as many native methods in your Java code
     * as you want, their implementation is searched in the
     * currently loaded native libraries only the first time
     * you call them.
     *
     * Trying to call this function will result in a
     * java.lang.UnsatisfiedLinkError exception !
     */
    public native String unimplementedStringFromJNI();

    /* this is used to load the 'hello-jni' library on application
     * startup. The library has already been unpacked into
     * /data/data/com.example.HelloJni/lib/libhello-jni.so at
     * installation time by the package manager.
     */
    public native int[]  mydes1encJNI(int[] indata,int[] snr);

   
    static {
        System.loadLibrary("DHJni");
    }
}
